package com.liukai.user.presenter.impl;

import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_commons.API;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.RetrofitManager;
import com.liukai.user.call_back.IUserCallBack;
import com.liukai.user.presenter.IUserPresenter;

import javax.net.ssl.HttpsURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UserPresenterImpl extends BasePresenter<IUserCallBack> implements IUserPresenter {

    @Override
    public void login(UserInfo userInfo) {
        Retrofit retrofit = RetrofitManager.getUserInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> loginTask = api.login(userInfo);
        loginTask.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                // 返回码为200，登陆成功
                if (response.code() == HttpsURLConnection.HTTP_OK) {
                    Result result = response.body();
                    mCallBack.loginResult(result);
                }
                LogUtils.d(getClass(),response.body().toString());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

}
